﻿@using UsedBookTradeSystem.Models.Entity
@{
    ViewData["Title"] = "Add";
    Layout = "~/Views/Shared/_BackLayout.cshtml";
}

<div class="container-fluid flex-grow-1 container-p-y">
    <h4 class="font-weight-bold py-3 mb-0">书籍管理</h4>

    <div class="card mb-4">
        <h6 class="card-header">添加书籍</h6>
        <div class="card-body">
            <form action="/BookInfo/AddBookInfo" method="post" enctype="multipart/form-data">
                <div class="form-group row">
                    <label class="col-form-label col-sm-2 text-sm-right">书籍名称</label>
                    <div class="col-sm-10">
                        <input type="text" class="form-control" name="BookName" placeholder="请输入书籍名称" required />
                        <div class="clearfix"></div>
                    </div>
                </div>
                <div class="form-group row">
                    <label class="col-form-label col-sm-2 text-sm-right">作者</label>
                    <div class="col-sm-10">
                        <input type="text" class="form-control" name="Author" placeholder="请输入作者" required />
                        <div class="clearfix"></div>
                    </div>
                </div>
                <div class="form-group row">
                    <label class="col-form-label col-sm-2 text-sm-right">类型</label>
                    <div class="col-sm-10">
                        <select class="custom-select" name="Category">
                            @foreach (BookCategory item in ViewBag.CategoryList)
                            {
                                <option value="@item.Id">@item.CategoryName</option>
                            }
                        </select>
                        <div class="clearfix"></div>
                    </div>
                </div>
                <div class="form-group row">
                    <label class="col-form-label col-sm-2 text-sm-right">书籍详情</label>
                    <div class="col-sm-10">
                        <input type="text" class="form-control" name="BookDetail" placeholder="请输入书籍详情" required />
                        <div class="clearfix"></div>
                    </div>
                </div>
                <div class="form-group row">
                    <label class="col-form-label col-sm-2 text-sm-right">书籍规格</label>
                    <div class="col-sm-10">
                        <input type="text" class="form-control" name="Bookspecs" placeholder="请输入书籍规格" required />
                        <div class="clearfix"></div>
                    </div>
                </div>
                <div class="form-group row">
                    <label class="col-form-label col-sm-2 text-sm-right">出版社</label>
                    <div class="col-sm-10">
                        <input type="text" class="form-control" name="Press" placeholder="请输入出版社名称" required />
                        <div class="clearfix"></div>
                    </div>
                </div>
                <div class="form-group row">
                    <label class="col-form-label col-sm-2 text-sm-right">价格</label>
                    <div class="col-sm-10">
                        <input type="number" class="form-control" name="Price" step="0.1" min="1" max="9999" oninput="validatePrice(this)" placeholder="请输入价格" required />
                        <div class="clearfix"></div>
                    </div>
                </div>
                <div class="form-group row">
                    <label class="col-form-label col-sm-2 text-sm-right">库存</label>
                    <div class="col-sm-10">
                        <input type="number" class="form-control" name="Inventory" step="1" min="1" max="9999" oninput="validateInventory(this)" placeholder="请输入库存" required />
                        <div class="clearfix"></div>
                    </div>
                </div>
                <div class="form-group row">
                    <label class="col-form-label col-sm-2 text-sm-right">封面</label>
                    <div class="col-sm-10">
                        <input type="file" class="form-control" name="Picture" accept="image/gif,image/png" required />
                        <div class="clearfix"></div>
                    </div>
                </div>
                <div class="form-group row">
                    <div class="col-sm-10 ml-sm-auto">
                        <button type="submit" class="btn btn-success">确定</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
<script>
    function validatePrice(input) {
        var value = input.value;
        var decimalPart = value.split('.')[1];
        if (decimalPart && decimalPart.length > 2) {
            input.value = value.slice(0, -1); // 删除最后一个字符
            alert('小数位数不能超过两位！');
        }
    }
    function validateInventory(input) {
        var value = input.value;
        var decimalPart = value.split('.')[1];
        if (decimalPart && decimalPart.length > 0) {
            input.value = value.slice(0, -1); // 删除最后一个字符
            alert('不能有小数！');
        }
        var underPart = value.split('-')[1];
        if (underPart && underPart.length > 0) {
            input.value = value.slice(0, 1); // 删除第一个字符
            alert('不能有小数！');
        }
    }
</script>
